import request, {type ApiResponse} from '@/utils/request'
import type {
  ICaptcha,
  IDeptSaveParams,
  IDeptUpdateParams, IDictOptionParams, IDictParams,
  ILoginParams,
  ILoginResponse, ISysFormDTO, ISysMenuDTO, ISysUserDTO, ISysUserPswDTO
} from "@/types";
import type {IRoleUrlDTO, ISysUrlWhitelistDTO} from "@/types/sys";


//获取AES公钥
export function getRsaPublicKey(): Promise<ApiResponse<string>> {
  return request({
    url: '/api/sys/getRsaPublicKey',
    headers: {
      "ignoreToken": true
    },
    method: 'get',
  });
}

export function getCaptcha(): Promise<ApiResponse<ICaptcha>> {
  return request({
    url: '/api/sys/user/imageCaptcha',
    headers: {
      "ignoreToken": true
    },
    method: 'get',
  });
}


export function login(data: ILoginParams): Promise<ApiResponse<ILoginResponse>> {
  return request({
    url: '/api/sys/user/login',
    method: 'post',
    headers: {
      "ignoreToken": true
    },
    data
  });
}

export function logout() {
  return request({
    url: '/api/sys/user/logout',
    method: 'post',
  });
}

export function deptList() {
  return request({
    url: '/api/sys/dept/list',
    method: 'get',
  })
}

export function saveDept(data: IDeptSaveParams) {
  return request({
    url: '/api/sys/dept/save',
    method: 'post',
    data: data,
  })
}

export function updateDept(data: IDeptUpdateParams) {
  return request({
    url: '/api/sys/dept/update/' + data.id,
    method: 'post',
    data: data,
  })
}

export function removeDept(id: string) {
  return request({
    url: '/api/sys/dept/remove/' + id,
    method: 'post',
  })
}

export function createDict(data: IDictParams) {
  return request({
    url: '/api/sys/dict/create',
    method: 'post',
    data
  })
}

export function updateDict(id: number, data: IDictParams) {
  return request({
    url: '/api/sys/dict/update/' + id,
    method: 'post',
    data
  })
}

export function deleteDict(id: string) {
  return request({
    url: '/api/sys/dict/delete/' + id,
    method: 'post',
  })
}

export function createDictOption(data: IDictOptionParams) {
  return request({
    url: '/api/sys/dict/option/create',
    method: 'post',
    data
  })
}

export function updateDictOption(id: number, data: IDictOptionParams) {
  return request({
    url: '/api/sys/dict/option/update/' + id,
    method: 'post',
    data
  })
}

export function deleteDictOption(id: string) {
  return request({
    url: '/api/sys/dict/option/delete/' + id,
    method: 'post',
  })
}


export function addRolePermission(data: IRoleUrlDTO) {
  return request({
    url: '/api/sys/role/addUrl',
    method: 'post',
    data
  })
}

export function removeRolePermission(data: IRoleUrlDTO) {
  return request({
    url: '/api/sys/role/removeUrl',
    method: 'post',
    data
  })
}


export function addUrlToWhitelist(data: ISysUrlWhitelistDTO[]) {
  return request({
    url: '/api/sys/url/addUrlToWhitelist',
    method: 'post',
    data
  })
}


export function removeUrlFormWhitelist(data: string[]) {
  return request({
    url: '/api/sys/url/removeUrlFormWhitelist',
    method: 'post',
    data
  })
}


export function createMenu(data: ISysMenuDTO) {
  return request({
    url: '/api/sys/menu/create',
    method: 'post',
    data
  })
}

export function updateMenu(id: number, data: ISysMenuDTO) {
  return request({
    url: '/api/sys/menu/update/' + id,
    method: 'post',
    data
  })
}


export function getMenuByMenuId(id: number) {
  return request({
    url: '/api/sys/menu/queryById/' + id,
    method: 'get',
  })
}

export function deleteMenuByMenuId(id: number) {
  return request({
    url: '/api/sys/menu/deleteById/' + id,
    method: 'post',
  })
}

export function listAllMenus() {
  return request({
    url: '/api/sys/menu/listAll',
    method: 'get',
  })
}

export function getRoleMenuIds(id: string) {
  return request({
    url: '/api/sys/menu/getRoleMenuIds/' + id,
    method: 'get',
  })
}

export function updateRoleMenus(id: string, data: number[]) {
  return request({
    url: '/api/sys/menu/updateRoleMenus/' + id,
    method: 'post',
    data
  })
}

export function queryWebInfo() {
  return request({
    url: '/api/sys/webInfo',
    method: 'get',
  })
}

export function updateSelfPwd(data: ISysUserPswDTO) {
  return request({
    url: '/api/sys/user/changePassword',
    method: 'post',
    data
  })
}


export function saveUser(data: ISysUserDTO) {
  return request({
    url: '/api/sys/user/save',
    method: 'post',
    data
  })
}

export function updateUser(id, data: ISysUserDTO) {
  return request({
    url: '/api/sys/user/update/' + id,
    method: 'post',
    data
  })
}


export function deleteUser(id) {
  return request({
    url: '/api/sys/user/delete/' + id,
    method: 'post',
  })
}


export function queryRoleList() {
  return request({
    url: '/api/sys/role/query',
    method: 'get',
  });
}

export function queryDict(code: string) {
  return request({
    url: '/api/sys/dict/query/' + code,
    method: 'get',
  });
}

export function queryRoleMenu() {
  return request({
    url: '/api/sys/menu/roleMenu',
    method: 'get',
  });
}

export function loadSysForm(id: string) {
  return request({
    url: '/api/sys/form/load/' + id,
    method: 'get',
  })
}

export function checkSaveSysForm(id: string) {
  return request({
    url: '/api/sys/form/saveCheck/' + id,
    method: 'get',
  })
}

export function saveSysForm(data: ISysFormDTO) {
  return request({
    url: '/api/sys/form/save',
    method: 'post',
    data
  })
}

